package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.domain.DataNode;
import com.example.config.auth.SecurityUtil;
import com.example.config.exception.GlobalExcetion;
import com.example.domain.vo.BusReceiveMsgVo;
import com.example.feign.NodeFeignService;
import com.example.service.BusSystemReceivemsgRecordService;
import com.example.system.domain.BusSystemReceivemsgRecord;
import com.example.system.domain.BusSystemTableUser;
import com.example.system.service.BusSystemTableUserService;
import com.example.util.Results;
import com.example.util.sql.IdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//发消息记录
@RestController
@RequestMapping("receiverecord")
public class ReceiveMsgRecordController {

    @Value("${spring.application.name}")
    private String appName;

    private static final String SYSTEM_RECEIVE_MSG = "bus_system_receivemsg_record";

    @Autowired
    private BusSystemTableUserService busSystemTableUserService;

    @Autowired
    private NodeFeignService nodeFeignService;

    @Autowired
    private BusSystemReceivemsgRecordService busSystemReceivemsgRecordService;


    //创建消息记录
    @PostMapping("/create/receivemsgrecord")
    public JSONObject createReceiveMsgRecord(@RequestBody BusReceiveMsgVo vo) throws GlobalExcetion {
        BusSystemTableUser busSystemTable=busSystemTableUserService.selectByUserId(vo.getFromUid(),SYSTEM_RECEIVE_MSG);
        if(busSystemTable!=null){
            DataNode dataNode =nodeFeignService.getDataNodeConfigByServerName(appName);
            BusSystemReceivemsgRecord bus=new BusSystemReceivemsgRecord();
            BeanUtils.copyProperties(vo,bus);
            bus.setUuid(IdUtils.getUUID());
            bus.setCreateTime(new Date());
            bus.setUpdateTime(new Date());
            bus.setToNodeid(dataNode.getId());
            bus.setDsNo(dataNode.getId());
            bus.setIsDeleted(0);
            String saveTable= busSystemTable.getSaveTableName();
            busSystemReceivemsgRecordService.saveBusSystemReceivemsgRevord(saveTable,bus);
            return Results.success("发送记录创建成功");
        }
        return Results.error("发送记录创建成功");
    }

    /**
     * 获取朋友发给我的
     * @return
     * @throws GlobalExcetion
     */
    @GetMapping("/getmsglist/bytouid")
    public JSONObject getMsgListBytouid(@RequestParam("touid")String touid) throws GlobalExcetion {
        String userId= SecurityUtil.getUserId();
        BusSystemTableUser tableUser=busSystemTableUserService.selectByUserId(userId,SYSTEM_RECEIVE_MSG);
        String queryTable=tableUser.generateUnionAllTable("from_uid");
        List<BusSystemReceivemsgRecord> list=busSystemReceivemsgRecordService.getList(queryTable,userId,touid);
        return Results.success(list);
    }

    //获取消息记录列表
    @GetMapping("/getmsglist")
    public JSONObject getMsgList() throws GlobalExcetion {
        String userId= SecurityUtil.getUserId();
        BusSystemTableUser tableUser=busSystemTableUserService.selectByUserId(userId,SYSTEM_RECEIVE_MSG);
        String queryTable=tableUser.generateUnionAllTable("from_uid");
        List<BusSystemReceivemsgRecord> list=busSystemReceivemsgRecordService.getList(queryTable,userId);
        return Results.success(list);
    }

    @GetMapping("/getmsgpage")
    public JSONObject getMsgPage(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
        String userId= SecurityUtil.getUserId();
        //获取用户的分表
        BusSystemTableUser tableUser=busSystemTableUserService.selectByUserId(userId,SYSTEM_RECEIVE_MSG);
        //构建虚拟表通过用户id
        String queryTable=tableUser.generateUnionAllTable("from_uid");
        IPage<BusSystemReceivemsgRecord> page=busSystemReceivemsgRecordService.getPage(queryTable,userId,new Page(pageNum,pageSize));
        return Results.success(page);
    }

    @GetMapping("/getmsgpage/bytouid")
    public JSONObject getMsgPageBytouid(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                        @RequestParam(value = "toUid")String toUid) throws GlobalExcetion {
        String userId= SecurityUtil.getUserId();
        //获取用户的分表
        BusSystemTableUser tableUser=busSystemTableUserService.selectByUserId(userId,SYSTEM_RECEIVE_MSG);
        //构建虚拟表通过用户id
        String queryTable=tableUser.generateUnionAllTable("from_uid");
        IPage<BusSystemReceivemsgRecord> page=busSystemReceivemsgRecordService.getPage(queryTable,userId,toUid,new Page(pageNum,pageSize));
        return Results.success(page);
    }
}
